package com.lovely602.ai.alibaba.controller.multimodality;


import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.content.Media;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.MimeType;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.net.URI;
import java.util.Base64;

/**
 * 多模态控制类
 * @author lizhixing
 */
@Slf4j
@RestController
public class MultiModalityController {

    private final ChatClient chatClient;

    public MultiModalityController(ChatClient.Builder builder) {

        this.chatClient = builder
                .defaultSystem("你是一名图片分析师")
                .build();
    }

    /**
     * 多模态测试，存在异常
     */
    @GetMapping(value = "/multiModality")
    public String multiModality(@RequestParam(value = "msg", defaultValue = "说说你在这个图片看到了什么？", required = true) String msg) throws IOException {

        ClassPathResource resource = new ClassPathResource("/multimodal.png");
        //第一步，图片转码：通过Base64编码将图片转化为字符串
        byte[] bytes = resource.getContentAsByteArray();
        String base64Data = Base64.getEncoder().encodeToString(bytes);

        URI uri = URI.create("https://hsimage.fotile.com/fotile/open/202509051545525432720.png");

        Media media = Media.builder()
                .mimeType(MimeType.valueOf(MimeTypeUtils.IMAGE_PNG_VALUE))
                .data(uri)
                .build();

        UserMessage userMessage = UserMessage.builder()
                .text(msg)
                .media(media)
                .build();

        DashScopeChatOptions chatOptions = DashScopeChatOptions.builder()
                .withModel("qwen-vl-max-2025-08-13")
                .build();

        Prompt prompt = new Prompt(userMessage, chatOptions);

        return chatClient.prompt(prompt).call().content();
    }

}
